#include "StdAfx.h"
#include "ConsoleForm.h"
#include "LogManagerInterface.h"
#include "Mainform.h"

using namespace OgreUnified;

ConsoleForm::ConsoleForm(void):mLogManagerInterface(0)
{
	InitializeComponent();
	Instance = this;
	addListener();
	mAddMessageDelegate = gcnew AddMessage(this, &ConsoleForm::addMessage);
}

ConsoleForm::~ConsoleForm()
{
	if (components)
	{
		delete components;
	}
}

void ConsoleForm::removeListener()
{
	if(mLogManagerInterface)
		Ogre::LogManager::getSingletonPtr()->getDefaultLog()->removeListener(mLogManagerInterface);
}

void ConsoleForm::addListener()
{
	mLogManagerInterface = new LogManagerInterface();
	Ogre::LogManager::getSingletonPtr()->getDefaultLog()->addListener(mLogManagerInterface);
}

void ConsoleForm::InitializeComponent(void)
{
	this->components = (gcnew System::ComponentModel::Container());
	System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(ConsoleForm::typeid));
	this->mToolStrip = (gcnew System::Windows::Forms::ToolStrip());
	this->mToolStripButtonNew = (gcnew System::Windows::Forms::ToolStripButton());
	this->mDataGridView = (gcnew System::Windows::Forms::DataGridView());
	this->mColumnIcon = (gcnew System::Windows::Forms::DataGridViewImageColumn());
	this->mColumnLog = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
	this->mSplitter = (gcnew System::Windows::Forms::Splitter());
	this->mRichTextBox = (gcnew System::Windows::Forms::RichTextBox());
	this->mImageList = (gcnew System::Windows::Forms::ImageList(this->components));
	this->mToolStrip->SuspendLayout();
	(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->mDataGridView))->BeginInit();
	this->SuspendLayout();
	// 
	// mToolStrip
	// 
	this->mToolStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->mToolStripButtonNew});
	this->mToolStrip->Location = System::Drawing::Point(0, 0);
	this->mToolStrip->Name = L"mToolStrip";
	this->mToolStrip->Size = System::Drawing::Size(424, 25);
	this->mToolStrip->TabIndex = 0;
	this->mToolStrip->Text = L"toolStrip1";
	// 
	// mToolStripButtonNew
	// 
	this->mToolStripButtonNew->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
	this->mToolStripButtonNew->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"mToolStripButtonNew.Image")));
	this->mToolStripButtonNew->ImageTransparentColor = System::Drawing::Color::Magenta;
	this->mToolStripButtonNew->Name = L"mToolStripButtonNew";
	this->mToolStripButtonNew->Size = System::Drawing::Size(23, 22);
	this->mToolStripButtonNew->Text = L"&Clear";
	this->mToolStripButtonNew->Click += gcnew System::EventHandler(this, &ConsoleForm::mToolStripButtonNew_Click);
	// 
	// mDataGridView
	// 
	this->mDataGridView->AllowUserToAddRows = false;
	this->mDataGridView->AllowUserToDeleteRows = false;
	this->mDataGridView->AutoSizeColumnsMode = System::Windows::Forms::DataGridViewAutoSizeColumnsMode::Fill;
	this->mDataGridView->AutoSizeRowsMode = System::Windows::Forms::DataGridViewAutoSizeRowsMode::AllCells;
	this->mDataGridView->CellBorderStyle = System::Windows::Forms::DataGridViewCellBorderStyle::None;
	this->mDataGridView->ColumnHeadersBorderStyle = System::Windows::Forms::DataGridViewHeaderBorderStyle::None;
	this->mDataGridView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
	this->mDataGridView->ColumnHeadersVisible = false;
	this->mDataGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(2) {this->mColumnIcon, 
		this->mColumnLog});
	this->mDataGridView->Cursor = System::Windows::Forms::Cursors::Arrow;
	this->mDataGridView->Dock = System::Windows::Forms::DockStyle::Fill;
	this->mDataGridView->Location = System::Drawing::Point(0, 25);
	this->mDataGridView->MultiSelect = false;
	this->mDataGridView->Name = L"mDataGridView";
	this->mDataGridView->RowHeadersVisible = false;
	this->mDataGridView->SelectionMode = System::Windows::Forms::DataGridViewSelectionMode::FullRowSelect;
	this->mDataGridView->Size = System::Drawing::Size(424, 339);
	this->mDataGridView->TabIndex = 2;
	this->mDataGridView->RowsAdded += gcnew System::Windows::Forms::DataGridViewRowsAddedEventHandler(this, &ConsoleForm::mDataGridView_RowsAdded);
	this->mDataGridView->CurrentCellChanged += gcnew System::EventHandler(this, &ConsoleForm::mDataGridView_CurrentCellChanged);
	// 
	// mColumnIcon
	// 
	this->mColumnIcon->FillWeight = 15.22843F;
	this->mColumnIcon->HeaderText = L"LogIcon";
	this->mColumnIcon->Name = L"mColumnIcon";
	this->mColumnIcon->Resizable = System::Windows::Forms::DataGridViewTriState::True;
	// 
	// mColumnLog
	// 
	this->mColumnLog->FillWeight = 184.7716F;
	this->mColumnLog->HeaderText = L"LogTest";
	this->mColumnLog->Name = L"mColumnLog";
	this->mColumnLog->ReadOnly = true;
	this->mColumnLog->Resizable = System::Windows::Forms::DataGridViewTriState::False;
	this->mColumnLog->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;
	// 
	// mSplitter
	// 
	this->mSplitter->Cursor = System::Windows::Forms::Cursors::SizeNS;
	this->mSplitter->Dock = System::Windows::Forms::DockStyle::Bottom;
	this->mSplitter->Location = System::Drawing::Point(0, 364);
	this->mSplitter->Name = L"mSplitter";
	this->mSplitter->Size = System::Drawing::Size(424, 3);
	this->mSplitter->TabIndex = 3;
	this->mSplitter->TabStop = false;
	// 
	// mRichTextBox
	// 
	this->mRichTextBox->Dock = System::Windows::Forms::DockStyle::Bottom;
	this->mRichTextBox->Location = System::Drawing::Point(0, 367);
	this->mRichTextBox->Name = L"mRichTextBox";
	this->mRichTextBox->Size = System::Drawing::Size(424, 31);
	this->mRichTextBox->TabIndex = 4;
	this->mRichTextBox->Text = L"";
	this->mRichTextBox->Enter += gcnew System::EventHandler(this, &ConsoleForm::mRichTextBox_Enter);
	// 
	// mImageList
	// 
	this->mImageList->ImageStream = (cli::safe_cast<System::Windows::Forms::ImageListStreamer^  >(resources->GetObject(L"mImageList.ImageStream")));
	this->mImageList->TransparentColor = System::Drawing::Color::Transparent;
	this->mImageList->Images->SetKeyName(0, L"error.png");
	this->mImageList->Images->SetKeyName(1, L"icon.png");
	this->mImageList->Images->SetKeyName(2, L"warning.png");
	// 
	// ConsoleForm
	// 
	this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
	this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
	this->ClientSize = System::Drawing::Size(424, 398);
	this->Controls->Add(this->mDataGridView);
	this->Controls->Add(this->mSplitter);
	this->Controls->Add(this->mRichTextBox);
	this->Controls->Add(this->mToolStrip);
	this->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
		static_cast<System::Byte>(0)));
	this->HideOnClose = true;
	this->Name = L"ConsoleForm";
	this->Text = L"ConsoleForm";
	this->mToolStrip->ResumeLayout(false);
	this->mToolStrip->PerformLayout();
	(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->mDataGridView))->EndInit();
	this->ResumeLayout(false);
	this->PerformLayout();

}

void ConsoleForm::logMessage(System::String^ message, LOG_TYPE logtype)
{
	 if (mDataGridView->InvokeRequired)
	 {
		 cli::array<System::Object^> ^a = gcnew cli::array<System::Object^> {message,(int) logtype};
		 mDataGridView->BeginInvoke(mAddMessageDelegate,a);
	 }
	 else// if(mDataGridView->Visible)
		addMessage(message,logtype);
}

void ConsoleForm::logMessage(System::String^ message)
{
	logMessage(message, LOG_INFO );
}

void ConsoleForm::logMessage(const Ogre::String& message)
{
	logMessage(gcnew System::String(message.c_str()));	
}

void ConsoleForm::addMessage(System::String ^ message, LOG_TYPE logtype)
{
	System::Drawing::Image^ image = nullptr;
	switch(logtype)
	{
	case LOG_ERROR:
		image = mImageList->Images[0];
		break;
	case LOG_INFO:
		image = mImageList->Images[1];
		break;
	case LOG_WARNING:
		image = mImageList->Images[2];
		break;
	}
	mDataGridView->Rows->Add(image, message);
	
}

System::Void ConsoleForm::mDataGridView_CurrentCellChanged(System::Object^  sender, System::EventArgs^  e)
{
	if(mDataGridView->SelectedRows->Count>0)
	{
		DataGridViewRow^ row = mDataGridView->SelectedRows[0];
		if(row->Cells->Count>0)
		{
			System::Windows::Forms::DataGridViewTextBoxCell ^ cell =(System::Windows::Forms::DataGridViewTextBoxCell ^) row->Cells[1];
			mRichTextBox->Text = (System::String^)cell->Value;
		}
	}	
}

System::Void ConsoleForm::mToolStripButtonNew_Click(System::Object^  sender, System::EventArgs^  e)
{
	mDataGridView->Rows->Clear();
	mRichTextBox->Clear();
}

System::Void ConsoleForm::mRichTextBox_Enter(System::Object^  sender, System::EventArgs^  e)
{
	for each(DataGridViewRow^ row in mDataGridView->SelectedRows)
		row->Selected=false;

}

System::Void ConsoleForm::mDataGridView_RowsAdded(System::Object^  sender, System::Windows::Forms::DataGridViewRowsAddedEventArgs^  e)
{
	//mDataGridView->Currentrowin = mDataGridView->RowCount - 1;
//	if(mDataGridView->SelectedRows->Count==0)
//		mDataGridView->FirstDisplayedScrollingRowIndex = mDataGridView->RowCount - 1;
}
